<?php

namespace App\Http\Controllers\Backend;

use App\Http\Requests\Backend\StoreWechatMessage;
use App\Http\Resources\Backend\WechatMessageResource;
use App\Models\AdminLog;
use App\Models\WechatMessage;
use Illuminate\Http\Request;

/**
 * Class WechatMessageController
 * @package App\Http\Controllers\Backend
 * 后台管理自动回复规则
 */
class WechatMessageController extends BackendController
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     * 消息回复列表
     */
    public function index(Request $request)
    {
        $wechatMessage = WechatMessage::orderBy('sort', 'asc');
        if ($request->type == WechatMessage::RESPONSE_TYPE_KEY) {
            $wechatMessage = $wechatMessage->responseTypeKey();
        } elseif ($request->type == WechatMessage::RESPONSE_TYPE_RECEIVE) {
            $wechatMessage = $wechatMessage->responseTypeReceive();
        } elseif ($request->type == WechatMessage::RESPONSE_TYPE_SUBSCRIBE) {
            $wechatMessage = $wechatMessage->responseTypeSubscribe();
        }
        $wechatMessage = $wechatMessage->nameLike($request->name)->paginate($request->size);
        return WechatMessageResource::collection($wechatMessage);
    }

    /**
     * @param StoreWechatMessage $request
     * @return WechatMessageResource
     * 添加消息回复
     */
    public function store(StoreWechatMessage $request)
    {
        $wechatMessage = WechatMessage::create($request->all());
        if ($wechatMessage->type == WechatMessage::RESPONSE_TYPE_KEY) {
            AdminLog::log('新增关键字规则' . $wechatMessage->name);
        }
        if ($wechatMessage->type == WechatMessage::RESPONSE_TYPE_RECEIVE) {
            AdminLog::log('新增收到消息回复规则' . $wechatMessage->name);
        }
        if ($wechatMessage->type == WechatMessage::RESPONSE_TYPE_SUBSCRIBE) {
            AdminLog::log('新增被关注消息回复规则' . $wechatMessage->name);
        }
        return new WechatMessageResource($wechatMessage);
    }

    /**
     * @param StoreWechatMessage $request
     * @param WechatMessage $wechatMessage
     * @return WechatMessageResource
     * 编辑消息回复
     */
    public function update(StoreWechatMessage $request, WechatMessage $wechatMessage)
    {
        $wechatMessage->fill($request->all());
        $wechatMessage->save();
        if ($wechatMessage->type == WechatMessage::RESPONSE_TYPE_KEY) {
            AdminLog::log('修改关键字规则' . $wechatMessage->name);
        }
        if ($wechatMessage->type == WechatMessage::RESPONSE_TYPE_RECEIVE) {
            AdminLog::log('修改收到消息回复规则' . $wechatMessage->name);
        }
        if ($wechatMessage->type == WechatMessage::RESPONSE_TYPE_SUBSCRIBE) {
            AdminLog::log('修改被关注回复规则' . $wechatMessage->name);
        }
        return new WechatMessageResource($wechatMessage);
    }

    /**
     * @param WechatMessage $wechatMessage
     * @return WechatMessageResource
     * @throws \Exception
     * 删除消息回复
     */
    public function destroy(WechatMessage $wechatMessage)
    {
        $wechatMessage->delete();
        return new WechatMessageResource($wechatMessage);
    }
}